package com.example.xiaolu.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Arrays;
import java.util.Base64;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;

public class Test {
    private static final String relativePath = "src/main/resources/static/userAvatar";
    /**
     * 写入字符串数据到文件
     * @param data 要写入的字符串数据
     */
    public static void writeStringToFile(String data, String userId) {
        String path = relativePath + "/" + userId + "avatar.txt";
        try {
            Files.createDirectories(Paths.get(new File(path).getParent()));
            System.out.println(Files.write(Paths.get(path), data.getBytes(StandardCharsets.UTF_8)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 从文件中读取字符串数据
     * @return 读取的字符串数据
     */
    public static String readStringFromFile(String userId) {
        String path = relativePath + "/" + userId + "avatar.txt";
        try {
            // 从文件中读取数据
            byte[] bytes = Files.readAllBytes(Paths.get(path));
            return new String(bytes, StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 压缩 Base64 编码字符串
    public static String base64ToString(String base64String) {
        // 创建Base64解码器
        Base64.Decoder decoder = Base64.getDecoder();

        // 解码Base64字符串
        byte[] decodedBytes = decoder.decode(base64String);

        // 将字节数组转换为字符串
        String decodedString = new String(decodedBytes);

        // 输出解码后的字符串
        System.out.println("Decoded string: " + decodedString);

        return decodedString;
    }

    // 将压缩后的 Base64 字符串存储到数据库
    public static void bytesToBase64(String decodedString) {
        byte[] byteArray = decodedString.getBytes();
        // 2. 将解码后的数据重新编码为Base64
        Base64.Encoder encoder = Base64.getEncoder();
        String reEncodedString = encoder.encodeToString(byteArray);
        System.out.println("Re-encoded Base64 string: " + reEncodedString);
    }

    public static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
//        try {
//            // 示例 Base64 字符串
        String base64String = "iVBORw0KGgoAAAANSUhEUgAAArMAAAKzCAIAAACChRJMAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAgAElEQVR4nOy925osuYo0aCaPmv+p52JeYN63v0oXcwEGSO4embkOVWv3HvXuVZER7jogBAZCiP/P//3/QoVkfh4cBoNhzvnx8WFmgJEDwBiDg4MjnhwDAAlba/BiZmYGMwNgIGkwkv4kwd70Oc940r/MSgDk1/6iilfibeBS6N0C+q/R6N7T1pj/3voxzXA3Ohhu2/28BF00rjao7OG1ZurJJJLTNj5498wAvF4vsGg757x24L5fvS3viGaPGvE0m3P29/2BMUaQiBwk1DcAHKNGcSXjparsw3visjjozTPfmKGl9bf9/EpVpnqS/r3O0+LLOSeLu8UJ/qe+jfWoMjjMrMavWvs6uOfM5JNPO2+2UYPePcba7796T/yVaZPBqvHvGMPMPj4+5nli8Dpn4h+a1Vv1K4L9zBe7WNNsGjDnnOfpHfMy5zQzbxeAt97rHOJSujxCCahOyTknAQk9oq1Km3N9HBCT25znOf0VhpBU653yJQBsHMcy3k3amLn8gbnkHNmNEm4+hjlt2gxyFZlTsPjI5zw/ztOrHBz+q9PEvD9ayNsS2FjifRla8kwyGXxeorFGuTYis8YEe51kdiH5P9njnNNpJ7Fv9JGMMec8z9OXYXCFeAAGi4nmNJssxlavYWY3a0bdy+e95vM8Ow3RVlOX2yRzovuT0ELjpX4A55w25zRLiUriYLSVcthZxdfphOVPU5LcufHQAplznlpH3rfX3ZzGJMIwbapb3ueYZDa5o+GNprxXAgLmeiKkZcmyrETLPp+MQbPRsfXt0lXczVz9evPNk0oxGG35keh9Wsf13O4nJSfyTkzfyHQpGTQaazKYREXSbJFBjx24KdOiKhebnaHZ1itjAfkwIEka4sAMx1Gd+eFCH9sygo4XDXYzj7vAvp29hwZ/rJ9vS8KCJ7HK1sMFEEAUdDFXkx91hl5LZ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EEFA6jL3cMaLls7ulsQFd7+W346+hT2L7PbsTbTQZY8RKeW1wG27199YFMGEJpXCvyuEBmSGknBTpGqA6q3+H+L3cWAxaQPEItS/pbf8/YFJUAd6eHmdlpfZGVAjabxKbV6MJr+QJaKI0yt+tCs3pDSxMkiQ05F4U2idGAiFgjxGbsezCwu16qDSZngCAFJEvMHeBNyPbeht977/YPldAQrQIr4fUlIU1i+oQ/GgAJTFOHhwgo2an/9RcYHDMwNlRkc5b0uAHkWweXyQ5lnxDNCd4dsXqlsAI2ZheuajWQxKCbYfBtXHVtyLN05bhs+kkvdM3/Kg+w7xREvwjDwiGuLVw0kTQDB2yGGduovBLuYjRED7y9GZAszAvBggL1N/LcH+hS73aQ6/ObK+Kb5dcgi6okNiyi7hg1Gydvrwm4dZ2/POeqJXULLgbWymtr9fRVg3QDh6/YYGYZ+XFOOwbmBHw3bk5DbWIYLONOEhf/Sl18A9D/o8rCgWnD+j9WEGd7yUWaAIV9xoeuCdoyG53nsC3V7EHot+SxrMvQzDvWXK8vL701ahxdOkt9Y7L0sS9iV8DadxZFOnCINmu9WJLMQTNC7Lsoo+okFUOwsk8XHdqUd6XQXdNmwJBc4qCd8zzncXBbU7nQKEzz3mWViMkCzLu4V6sEpq8cuqE3zSamz+U08wkZjF+DxjFzJdY5CK1izY1DC0ufTVdhjmDdBghFdRzJsbsuees/KPXz8IwJhaK0e6v9vaQthUq+e7TYKWYw+ZnFyWowZXFo2XPOeN6hJBeeAeDbzd0H8Dj";
//        writeStringToFile(base64String, "1");
        System.out.println(readStringFromFile("1"));
        //
//            // 压缩 Base64 字符串
//            String compressedBase64 = compressBase64(base64String);
//            System.out.println("Compressed Base64: " + compressedBase64);
//
//            // 存储到数据库
////            saveCompressedBase64ToDatabase(compressedBase64);
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        // 假设这是一个Base64编码的字符串
//        String base64String = "SGVsbG8sIFdvcmxkIQ==";


    }
}

